<?php

/*
按钮
*/

namespace app\admin\controller;


use app\admin\base\JwBase;
use think\Exception;
use think\facade\Validate;


class Menu extends JwBase
{
    /**
     * info: 获取菜单
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/9 17:57
     * Url: menus
     */
    public function menus()
    {
        $data = \app\model\Menu::menuList();
        $this->respInfo(1000, $data);
    }

    /**
     * info: 获取菜单
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/9 17:57
     * Url: menus
     */
    public function menus2()
    {
        $data = \app\model\Menu::menuListFinld();
        $this->respInfo(1000, $data);
    }

    /**
     * info: 数据添加
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/9 23:44
     * Url: addmenu
     */
    public function addmenu()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'icon|图标' => ['require', 'max' => 30, 'min' => 1],
                    'name|权限名称' => ['require', 'max' => 7, 'min' => 1],
                    'sid|上级ID' => ['require', 'number', 'max' => 6, 'min' => 1],
                    'site|地址' => ['require', 'max' => 30, 'min' => 1],
                    'sort|排序' => ['require', 'number', 'max' => 4, 'min' => 1],
                    'type|类型' => ['require', 'number', 'length' => 1],
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }
                if (isset($postData['mid']) && !empty($postData['mid'])) {
                    $dataMenu["mid"] = $postData['mid'];
                }

                $dataMenu["icon"] = $postData['icon'];
                $dataMenu["name"] = $postData['name'];
                $dataMenu["sid"] = $postData['sid'];
                $dataMenu["site"] = $postData['site'];
                $dataMenu["sort"] = $postData['sort'];
                $dataMenu["type"] = $postData['type'];

                $menu = $user = new \app\model\Menu();;
                $menu->save($dataMenu);
                if ($menu->mid > 0) {
                    $this->respInfo(1000);
                } else {
                    $this->respInfo(3000);
                }

            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 数据更新
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/9 23:43
     * Url: upmenu
     */
    public function upmenu()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                $rule = [
                    'mid|操作ID' => ['require', 'number', 'max' => 10, 'min' => 1],
                    'icon|图标' => ['require', 'max' => 30, 'min' => 1],
                    'name|权限名称' => ['require', 'max' => 7, 'min' => 1],
                    'sid|上级ID' => ['require', 'number', 'max' => 6, 'min' => 1],
                    'site|地址' => ['require', 'max' => 30, 'min' => 1],
                    'sort|排序' => ['require', 'number', 'max' => 4, 'min' => 1],
                    'type|类型' => ['require', 'number', 'length' => 1],
                ];
                $validate = Validate::rule($rule);
                if (!$validate->check($postData)) {
                    $this->respInfo(2500, [], $validate->getError());
                }

                $whereMenu["mid"] = $postData['mid'];
                $dataMenu["icon"] = $postData['icon'];
                $dataMenu["name"] = $postData['name'];
                $dataMenu["sid"] = $postData['sid'];
                $dataMenu["site"] = $postData['site'];
                $dataMenu["sort"] = $postData['sort'];
                $dataMenu["type"] = $postData['type'];

                $menu =  \app\model\Menu::find($postData['mid']);
                $menu->save($dataMenu);
                $this->respInfo(1000);

            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 获取级联
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/9 23:45
     * Url: getfather
     */
    public function getfather()
    {
        try {
            if (request()->isPost()) {
                $postData = input('post.');
                if (isset($postData['mid']) && !empty($postData['mid'])) {
                    $data = \app\model\Menu::getFather($postData['mid']);
                    $this->respInfo(1000, $data);
                } else {
                    $this->respInfo(2500, [], '参数错误');
                }
            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }

    /**
     * info: 权限删除
     * User: 伍先生
     * Argument:
     * DateTime: 2020/12/11 11:48
     * Url: delMenu
     */
    public function delMenu(){
        try {
            if (request()->isPost()) {
                $postData = input('post.');

                if (isset($postData['mid']) && !empty($postData['mid'])) {
                    $data = \app\model\Menu::getSon($postData['mid']);
                    array_push($data,$postData['mid']);
                    \app\model\Menu::destroy($data);
                    $this->respInfo(1000);
                } else {
                    $this->respInfo(2500, [], '参数错误');
                }
            } else {
                $this->respInfo(2001);
            }

        } catch (Exception $e) {
            $this->respInfo(12002, [], $e->getMessage());
        }
    }
}



